//
//  PullTableView.m
//  drivingSchool
//
//  Created by 子yu 刘 on 2018/11/15.
//  Copyright © 2018年 子yu 刘. All rights reserved.
//

#import "PullTableView.h"

@interface PullTableView ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)NSArray * dataArr;
@property (nonatomic ,strong)UITableView * tableView;

@end

@implementation PullTableView

+ (PullTableView *)sharedInstance{
    static  PullTableView * pullView;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        pullView = [[PullTableView alloc] init];
    });
    return pullView;
}

- (instancetype)init{
    if (self = [super init]) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI{
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self addSubview:tableView];
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_offset(0);
    }];
    self.tableView = tableView;
}

+ (void)pullWiteFrme:(CGRect)frame superView:(UIView *)superView index:(selectedBlock)block{
    [[PullTableView sharedInstance] pullViewWithFrame:frame superView:superView index:(selectedBlock)block];
}

- (void)pullViewWithFrame:(CGRect)frame superView:(UIView *)superView index:(selectedBlock)block{
    _block = block;
    self.frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, 150);
   // [self.tableView reloadData];
    [superView addSubview:self];
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 30;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cellId"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellId"];
        cell.textLabel.font = [UIFont systemFontOfSize:14];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    if (indexPath.row == _selIndex) {
        cell.textLabel.textColor = DEF_COLOR_MAIN;
    }else{
        cell.textLabel.textColor = [UIColor colorWithHexString:@"333333"];
    }
    cell.textLabel.text = self.dataArr[indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    _selIndex = indexPath.row;
    [tableView reloadData];
    if (_block) {
        _block(indexPath.row);
    }
    [self removeFromSuperview];
}

-(NSArray *)dataArr{
    if (!_dataArr) {
        _dataArr = @[@"最新上架",@"价格最高",@"价格最低",@"车龄最短",@"里程最少",];
    }
    return _dataArr;
}

@end
